package lineartable.test;

import lineartable.arraylist.SequentialList;
import lineartable.inter.LinearLinkedList;
import lineartable.inter.LinearList;
import lineartable.linkedlist.circularlinkedlist.CircularLinkedList;
import lineartable.linkedlist.doublylinkedlist.doublycircularlinkedlist.DoublyCircularLinkedList;

public class Main {
    public static void main(String[] args) {
        // 顺序存储结构
//        testArrayList();

        // 链式存储结构
        testLinkedList();
    }

    /**
     * 顺序存储结构测试
     */
    public static void testArrayList() {
        SequentialList list = new SequentialList();
        for (int i = 0; i < 10; i++) {
            list.listInsert(i, (char) ('a' + i));
        }
        linearListTest(list);
    }

    /**
     * 链式存储结构测试
     */
    public static void testLinkedList() {
        // 普通测试链表方法测试
//        LinearLinkedList list0 = new DoublyCircularLinkedList();
        LinearLinkedList list1 = new DoublyCircularLinkedList();
        // 头插法
//        for (int i = 0; i < 10; i++) {
//            list0.createListHead((char) ('a' + i));
//        }
//        linearListTest(list0);
//        System.out.println("------------------------------------------");
        // 尾插法
        for (int i = 0; i < 10; i++) {
            list1.createListTail((char) ('a' + i));
        }
        linearListTest(list1);
//        list1.createListTail('a');
//        System.out.println(list1.toString());
        // 特殊方法测试
        // 循环链表合并
//        ((CircularLinkedList) list0).addList((CircularLinkedList) list1);
//        linearListTest(list0);
//        linearListTest(list0);
        // 循环链表合并2
//        CircularLinkedList list = CircularLinkedList.margeList((CircularLinkedList) list0, (CircularLinkedList) list1);
//        linearListTest(list);
    }

    /**
     * 线性结构基本方法测试
     *
     * @param list
     */
    public static void linearListTest(LinearList list) {
        System.out.println("初始表");
        System.out.println(list.toString());
        System.out.println("链表长度：" + list.getListLength());
        System.out.println("删除位置2（从0开始）元素测试");
        list.listDelete(2);
        System.out.println(list.toString());
        System.out.println("链表长度：" + list.getListLength());
        System.out.println("插入位置2（从0开始）测试");
        list.listInsert(2, '1');
        System.out.println(list.toString());
        System.out.println("链表长度：" + list.getListLength());
        System.out.println("获取第1个位置(从0开始)元素");
        System.out.println(list.getElem(1));
        System.out.println("获取元素‘f’位置");
        System.out.println(list.locateElem('f'));
        System.out.println("清空表");
        System.out.println("清空前：isEmpty=" + list.listEmpty() + "；listLength=" + list.getListLength());
        list.clearList();
        System.out.println("清空后：isEmpty=" + list.listEmpty() + "；listLength=" + list.getListLength());
    }
}
